<?php
// 文章管理
namespace app\bbs_admin\controller;

use think\Controller;
use think\Db;

class Classifiedmanagement extends Controller
{
  public function index()
  {
    //初始化
    $head = bbs_admin_menu_hend();
    $articlelist_list_number = Db::table('bbs_settings')
      ->where(['id' => 1])
      ->field('articlelist_list_number')
      ->find()['articlelist_list_number'];
    //初始化

    $bbs_classification_main_count = Db::table('bbs_classification_main')
      ->count();
    $_page = input('page', 1);
    if ($_page > ceil($bbs_classification_main_count / $articlelist_list_number)) {
      $_page = ceil($bbs_classification_main_count / $articlelist_list_number);
    }
    if ($_page < 1) {
      $_page = 1;
    }
    $page = getPageHtml($bbs_classification_main_count, $articlelist_list_number, $_page, $_GET);
    getPageHtml($bbs_classification_main_count, 15, $_page);
    $bbs_classification_main = Db::table('bbs_classification_main')
      ->limit($_page * $articlelist_list_number - $articlelist_list_number, $articlelist_list_number)
      ->select();
    return $this->fetch('Classifiedmanagement', ['head' => $head, 'bbs_classification_main' => $bbs_classification_main, 'page' => $page]);
  }
  public function class_delete()
  {
    // $id = input('id');
    // db::table('bbs_classification_main')
    // ->update([
    //   'start'=>1
    // ]);
  }
  public function class_Unhide()
  {
    $id = input('id');
    $start = db::table('bbs_classification_main')
      ->where(['id' => $id])
      ->update([
        'start' => 0
      ]);
    if ($start) {
      $this->success('success');
    } else {
      $this->error('error');
    }
  }
  public function class_hide()
  {
    $id = input('id');
    $start = db::table('bbs_classification_main')
      ->where(['id' => $id])
      ->update([
        'start' => 1
      ]);
    if ($start) {
      $this->success('success');
    } else {
      $this->error('error');
    }
  }
  public function class_Subarticle_management()
  {
    $id = input('id');
    $data['list'] = db::table('bbs_classification_son')
      ->where([
        'main_class_id' => $id
      ])
      ->select();

    $this->success('success', null, $data);
  }
  public function son_class_Unhide()
  {
    $id = input('id');
    $start = Db::table('bbs_classification_son')
      ->where(['id' => $id])
      ->update(['son_class_start' => 0]);
    if ($start) {
      $this->success('success');
    } else {
      $this->error('error');
    }
  }
  public function son_class_hide()
  {
    $id = input('id');
    $start = Db::table('bbs_classification_son')
      ->where(['id' => $id])
      ->update(['son_class_start' => 1]);
    if ($start) {
      $this->success('success');
    } else {
      $this->error('error');
    }
  }
  public function son_class_Unrelease()
  {
    $id = input('id');
    $start = Db::table('bbs_classification_son')
      ->where(['id' => $id])
      ->update(['son_class_release' => 0]);
    if ($start) {
      $this->success('success');
    } else {
      $this->error('error');
    }
  }
  public function son_class_release()
  {
    $id = input('id');
    $start = Db::table('bbs_classification_son')
      ->where(['id' => $id])
      ->update(['son_class_release' => 1]);
    if ($start) {
      $this->success('success');
    } else {
      $this->error('error');
    }
  }
  public function category_management()
  {
    $where['son_class_name']  = input('get.son_class_name');
    $where['id']  = input('get.id');

    $head = bbs_admin_menu_hend();
    $bbs_classification_son =  Db::table('bbs_classification_son')
      ->where($where)
      ->count();
    if ($bbs_classification_son) {
      $bbs_article_class_list = Db::table('bbs_article_class')
        ->where([
          'bbs_classification_son_id' => $where['id'],
        ])->select();
    } else {
      $this->error('请从查看分类进入 错误行' . __LINE__);
    }

    return $this->fetch('category_management', ['head' => $head, 'bbs_article_class_list' => $bbs_article_class_list, 'get_id' => input('get.id')]);
  }
  public function delete_article_class_list()
  {
    $id = input('id');
    if ($id) {
      $start = Db::table('bbs_article_class')
        ->where(['id' => $id])->update(['start' => 1]);
      if ($start) {
        $this->success('success');
      } else {
        $this->error('发生意外错误');
      }
    } else {
      $this->error('缺少参数');
    }
  }
  public function recovery_article_class_list()
  {
    $id = input('id');
    if ($id) {
      $start = Db::table('bbs_article_class')
        ->where(['id' => $id])->update(['start' => 0]);
      if ($start) {
        $this->success('success');
      } else {
        $this->error('发生意外错误');
      }
    } else {
      $this->error('缺少参数');
    }
  }
  public function open_post_review()
  {
    $id = input('id');
    if ($id) {
      $start = Db::table('bbs_article_class')
        ->where(['id' => $id])->update(['to_examine' => 0]);
      if ($start) {
        $this->success('success');
      } else {
        $this->error('发生意外错误');
      }
    } else {
      $this->error('缺少参数');
    }
  }
  public function close_post_review()
  {
    $id = input('id');
    if ($id) {
      $start = Db::table('bbs_article_class')
        ->where(['id' => $id])->update(['to_examine' => 1]);
      if ($start) {
        $this->success('success');
      } else {
        $this->error('发生意外错误');
      }
    } else {
      $this->error('缺少参数');
    }
  }
  public function category_management_publishing_tasks()
  {
    $class_name = input('post.class_name');
    $inlineFormCustomSelectPref = input('post.inlineFormCustomSelectPref');
    $bbs_classification_son_id = input('post.bbs_classification_son_id');
    $start = Db::table('bbs_article_class')
      ->where([
        'bbs_classification_son_id' => $bbs_classification_son_id,
        'class_name' => $class_name,
        'start' => 0,
      ])
      ->find();
    if ($start) { 
      $this->error('您所发布的已存在');
    } else {
      $start = Db::table('bbs_article_class')
        ->insert([
          'bbs_classification_son_id' => $bbs_classification_son_id,
          'class_name' => $class_name,
          'to_examine' => $inlineFormCustomSelectPref,
          'start' => 0,
        ]);
      if ($start) {
        $this->success();
      } else {
        $this->error();
      }
    }
  }
}
